package net.takela.push.model;


import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import net.takela.common.web.model.Model;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by baboy on 13/06/2017.
 */
public class PushWeChatTplMsg extends Model {
    private String templateId;
    private String toOpenId;
    private String url;
    private Map data;

    public String getTemplateId() {
        return templateId;
    }

    public void setTemplateId(String templateId) {
        this.templateId = templateId;
    }

    public String getToOpenId() {
        return toOpenId;
    }

    public void setToOpenId(String toOpenId) {
        this.toOpenId = toOpenId;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Map getData() {
        return data;
    }

    public void setData(Map data) {
        this.data = data;
    }

    public void addData(String key, String value, String color){
        Map item = new HashMap();
        item.put("value", value);
        item.put("color", color);
        if(data == null){
            data = new HashMap();
        }
        data.put(key, item);
    }

    public String getMsgBody(){
        Map map = new HashMap();
        map.put("touser", getToOpenId());
        map.put("template_id", getTemplateId());
        map.put("url", getUrl());
        map.put("data", data);
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
        mapper.configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true);
        String s = null;
        try {
            s = mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return s;
    }
}
